function ajax(url, option) {
    url = url.trim()
    const method = (option.method || 'get').toUpperCase().trim()
    let dataStr = ''
    function formatParams(data) {
        if(typeof data != 'object') return ''
        let str = ''
        for (let key in data) {
            const value = data[key]
            str += `&${key}=${value}`
        }
        dataStr.replace('&', '')
        if(method == "GET"){
            url = url + '?' + str
        }else{
            dataStr = str
        }
    };
    formatParams(option.data)
    
    const xhr = new XMLHttpRequest()
    xhr.open(method, url)
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8")
    //#3.发送请求
    xhr.send(dataStr)
    //#4.监听xhr的readystate的变化，使用onreadystatechange函数
    xhr.onreadystatechange = function (e) {
        if (xhr.readyState == 4) {
            const res = JSON.parse(xhr.responseText)
            if (xhr.status >= 200 && xhr.status < 300) {
                if (typeof option.success == 'function') {
                    option.success(res)
                }
            } else {
                if (typeof option.error == 'function') {
                    option.error(res, xhr)
                }
            }

        }
    }

}
/*------------------------表单相关---------------------------- */
function initForm(formFieldOption) {
    for (let field in formFieldOption) {
        const selector = formFieldOption[field].selector
        const element = document.querySelectorAll(selector)
        if (element.length == 1) {
            formFieldOption[field].element = element[0]
        } else if (element.length > 1) {
            formFieldOption[field].element = Array.from(element)
        } else {
            formFieldOption[field].element = null
        }
    }
}

//表单设置值
function setFormValue(formFieldOption, data) {
    for (let field in formFieldOption) {
        const element = formFieldOption[field].element
        const dataKey = formFieldOption[field].dataKey
        const fieldValue = data[dataKey] || ''
        if (Array.isArray(element)) {
            if (element[0].type == 'checkbox') {//如果是checkbox
                const fieldValueList = Array.isArray(fieldValue) ? fieldValue : fieldValue.split(',')
                element.forEach(item => {
                    item.checked = fieldValueList.includes(item.value) ? true : false
                })
            } else if (element[0].type == "radio") {//如果是radio
                element.forEach(item => {
                    item.checked = item.value == data[dataKey] ? true : false
                })
            }
        } else {
            element.value = fieldValue.toString()
        }
    }
}

//表单获取值
function getFormValue(formFieldOption) {
    let data = {}
    for (let field in formFieldOption) {
        const element = formFieldOption[field].element
        if(!element) continue
        const dataKey = formFieldOption[field].dataKey
        if (Array.isArray(element)) {
            let res = []
            element.forEach(item => {
                if (item.checked) {
                    res.push(item.value)
                }
            })
            data[dataKey] = res.join(',')
        } else {
            if(element.type == 'checkbox' || element.type == "radio"){
                data[dataKey] = element.checked ? element.value : ''
            }else{
                data[dataKey] = element.value
            }
            
        }
    }
    return data
};
//清空表单数据
function clearFormValue(formFieldOption) {
    for (let field in formFieldOption) {
        const element = formFieldOption[field].element
        if (element) {
            if (Array.isArray(element)) {
                element.forEach(item => {
                    item.checked = false
                })
            } else {
                element.value = ''
            }
        }
    }
}